var express = require('express');
var router = express.Router();

const Rec = require('../models/Recommend')
const Films = require('../models/Films')

/* GET home page. */
router.get('/', async function(req, res) {
    if(!req.session.login){
        res.redirect("/user/login");
        return;
    }
    // 请求电影数据
    var mids = await Rec.select(req.session.uid)
    if(mids == undefined){
        res.render('recommend', {
            jfilms: {},
            login: req.session.login,
            username: req.session.username,
            nav:"推荐"
        })
        return ;
    }
    var mids_str = JSON.parse(JSON.stringify(mids))['rec'];
    console.log(mids_str);
    if(mids_str == undefined){
        res.render('recommend', {
            jfilms: {},
            login: req.session.login,
            username: req.session.username,
            nav:"推荐"
        })
    }else{
        var mids_list = mids_str.slice(1,-1).split(', ').map((x)=>{return Number(x)})   
        const films = await Films.queryList(mids_list)
        const jfilms = JSON.parse(JSON.stringify(films))
        res.render('recommend', {
            jfilms: jfilms,
            login: req.session.login,
            username: req.session.username,
            nav:"推荐"
        })
    }
    console.log(mids_str);

});


module.exports = router;